package com.itheima.exer.homework.hw1007.hwtest;

import com.itheima.exer.homework.hw1007.domain.Student;

import java.text.Collator;
import java.util.*;

/**
 * 1.新建Student类,(String name,Integer age,Integer score).
 * <p>
 * 2.创建TreeMap集合<Student,Integer>,排序规则:按照学生的分数排序,
 * 如果分数相同则按照年龄排序,如果年龄相同则按照姓名首字母排序.
 * <p>
 * 3.添加三组数据到TreeMap中,然后遍历打印学生信息以及对应的学号.
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap<Student, Integer> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i = o1.getScore() - o2.getScore();
                if (i == 0) {
                    i = o1.getAge() - o2.getAge();
                }
                if (i == 0) {
                    Collator collator = Collator.getInstance(Locale.CHINA);
                    i = collator.compare(o1.getName(), o2.getName());
                }
                return i;
            }
        });
        treeMap.put(new Student("张三", 23, 99), 1);
        treeMap.put(new Student("李四", 24, 85), 2);
        treeMap.put(new Student("王五", 25, 99), 3);
        Set<Map.Entry<Student, Integer>> entrySet = treeMap.entrySet();
        for (Map.Entry<Student, Integer> Entry : entrySet) {
            Student key = Entry.getKey();
            Integer value = Entry.getValue();
            System.out.println(key + "学号为:" + value);
        }
    }
}
